#ifndef ENTITY_H
#define ENTITY_H

#include <QObject>
#include <QGraphicsObject>
#include <QGraphicsSceneMouseEvent>
#include "component.h"
#include <QJsonObject>

class Entity : public QGraphicsObject
{
    Q_OBJECT
public:
    explicit Entity(QGraphicsItem *parent = nullptr);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
    QPainterPath shape() const override;
    ComponentList components();

    int id();
    void setID(const int& id);
    bool isValid();
    void addComponent(QSharedPointer<Component> com);
    QString name() const { return mName; }
    void setName(const QString& name) { mName = name; }
    bool canCopy() const { return mCanCopy; }
    void setCanCopy(const bool& on) { mCanCopy = on; }
    bool isRemoved() const { return mRemove; }
    void setRemoved(const bool& removed) { mRemove = removed; }
    bool canRemove() const { return mCanRemove; }
    void setCanRemove(const bool& ret) { mCanRemove = ret; }

    bool hasCrrent() const { return mHasCurrent; }
    void setHasCurrent(const bool& ret) { mHasCurrent = ret; }

    virtual QJsonObject toJsonObject();
    virtual void removeSelf();
    virtual void start() {}
    virtual void stop() {}
    virtual void tick(const int& ms) { Q_UNUSED(ms) }

signals:
    void selected(Entity* entity);
    void diselected(Entity* entiy);
    void scenePosChanged();
    void clicked();
    void selfRemoved();

public slots:

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;

private:
    ComponentList mComponents;
    int mId;
    bool mAboutClicked;
    QString mName;
    bool mCanCopy;
    bool mRemove;
    bool mCanRemove;
    bool mHasCurrent; // flag if is this node is working on the circuit lifetime.
};

typedef  QList<Entity*> EntityList;
typedef  QMap<int, Entity*> EntityMap;

#endif // ENTITY_H
